var game = {
    data:[],
    score:0,
    status:1,
    gameover:0,
    gamerunning:1,

    start: function(){
        game.gamerunning = game.status;
        game.score = 0; //游戏开始时分数清零
        game.data = [ //重新给数组进行赋值
                [0,0,0,0],
                [0,0,0,0],
                [0,0,0,0],
                [0,0,0,0]
        ]
        // 生成两个随机数
        game.randomNum();
        game.randomNum();
        // 生成游戏开始时的视图
        game.dataView();
    },
    randomNum:function(){
        for(;;){
            var r = Math.floor(Math.random()*4);//生成一个行
            var c = Math.floor(Math.random()*4);//生成一个列
            if(game.data[r][c] == 0){
                var num = Math.random()>0.5?2:4; 
                game.data[r][c] = num;
                break;//退出死循环
            }
        }
    },
    dataView:function(){    //更新视图的方法
        for (var r = 0; r < 4; r++) {
            for(var c = 0; c<4;c++){
                var div = document.getElementById("c" + r + c); 
                if(game.data[r][c] != 0){  
                    div.innerHTML = game.data[r][c];//给单元格进行赋值
                    div.className = "cell n" + game.data[r][c]; //改变样式
                }else{  
                    div.innerHTML = "";
                    div.className = "cell";
                }
            }
        }

        document.getElementById("score_01").innerText = game.score;

        if(game.gamerunning == game.gameover){  //如果游戏处于结束的阶段了，更新视图
            document.getElementById("gameover").style.display = "block";
            document.getElementById("score_02").innerText = game.score;
        }else{
            document.getElementById("gameover").style.display = "none";
        }
    },
    isgameover:function(){  //  判断游戏是否结束的方法

        for(var r = 0;r<4;r++){
            for(var c = 0;c<4;c++){
                if(game.data[r][c] == 0){ 
                    return false;
                }
                if(c<3){//检测同一行之间是否有相同
                    if(game.data[r][c] == game.data[r][c+1]){
                        return false;
                    }
                }
                if(r<3){//检测同一列之间是否有相同
                    if(game.data[r][c] == game.data[r+1][c]){
                        return false;
                    }
                }

            }
        }
        return true;//如果条件全都不满足，表示游戏结束
    },


    moveLeft:function(){
        var before = String(game.data);
        for(var r = 0;r<4;r++){
            game.moveLeftinRow(r);
        }
        var after = String(game.data);
        if(before != after){    //  若是前后不相等
            game.randomNum();   //生成一个随机数
            if(game.isgameover()){
                game.gamerunning == game.gameover;      //游戏状态改变为gameover
            }
            game.dataView();//更新视图
        }
    },
    moveLeftinRow:function(r){
        for(var c = 0;c<3;c++){
            var nextc = game.moveLeftNextRows(r,c);
            if(nextc != -1){
                if(game.data[r][c] == 0){//如果为0,则替代
                    game.data[r][c] = game.data[r][nextc];
                    game.data[r][nextc] = 0;
                    c--;
                }else if(game.data[r][c] == game.data[r][nextc]){
                    game.data[r][c] *= 2;
                    game.score += game.data[r][c];
                    game.data[r][nextc] = 0;
                }
            }else{//如果没找到
                break;
            }
        }
    },
    moveLeftNextRows:function(r,c){
        for (var i = c+1;i<4;i++) {
            if (game.data[r][i] != 0) {
                return i;
            }
        }
        return -1;
    },

    //  右移开始
    moveRight:function(){
        var before = String(game.data);
        for (var r = 0;r<4;r++) {//逐行比较
            game.moveRightinRow(r);
        }
        var after = String(game.data);
        if (before != after) {
            game.randomNum();//生成一个随机数
            if(game.isgameover()){//判断游戏是否结束
                game.gamerunning = game.gameover;
            }
            game.dataView();//更新视图
        }
    },
    moveRightinRow:function(r){
        for(var c = 3;c>0;c--){
            var nextc = game.moveRightNextRows(r,c);
            if (nextc != -1) {
                if(game.data[r][c] == 0){
                    game.data[r][c] = game.data[r][nextc];
                    game.data[r][nextc] = 0;
                    c++;
                }else if (game.data[r][c] == game.data[r][nextc]) {
                    game.data[r][c] *= 2;
                    game.score += game.data[r][c];
                    game.data[r][nextc] = 0;
                }
            }else{
                break;
            }
        }
    },
    moveRightNextRows:function(r,c){//找位置的函数
        for(var i = c-1;i>=0;i--){
            if (game.data[r][i] != 0) {
                return i;
            }
        }
        return -1;
    },

    moveTop:function(){
        var before = String(game.data);
//      移动的方法
        for(var c = 0;c<4;c++){//移动每一列
            game.moveTopinRow(c);
        }
        var after = String(game.data);

        if(before != after){
            game.randomNum();//生成一个随机数
            if(game.isgameover()){//如果游戏结束
                game.gamerunning = game.gameover;//改变游戏的状态
            }
            game.dataView();//移动完成数据更新
        }

    },
    moveTopinRow:function(c){//仅仅移动一行
        for(var r = 0;r<4;r++){
            var nextr = game.moveTopNextRows(r,c);
            if(nextr != -1){
                if(game.data[r][c] == 0){//如果为0，就替代
                    game.data[r][c] = game.data[nextr][c];//
                    game.data[nextr][c] = 0;
                    r--;
                }else if(game.data[r][c] == game.data[nextr][c]){
                    game.data[r][c] *= 2;
                    game.score += game.data[r][c];
                    game.data[nextr][c] = 0;
                }
            }else{
                break;
            }
        }
    },
    moveTopNextRows:function(r,c){
        for(var i = r + 1;i < 4;i++){
            if (game.data[i][c] != 0) {
                return i;
            }
        }
        return -1
    },
    //

    moveDown:function(){//下移的方法
        var before = String(game.data);

        for(var c = 0;c<4;c++){//移动每一列
            game.moveDowninRow(c);
        }
        var after = String(game.data);

        if(before != after){
            game.randomNum();
            if(game.isgameover()){//如果游戏结束
                game.gamerunning = game.gameover;
            }
            game.dataView();//移动完成数据更新
        }

    },
    moveDowninRow:function(c){
        for(var r = 3;r>=0;r--){
            var nextr = game.moveDownNextRows(r,c);
            if(nextr != -1){
                if(game.data[r][c] == 0){
                    game.data[r][c] = game.data[nextr][c];
                    game.data[nextr][c] = 0;
                    r++;
                }else if(game.data[r][c] == game.data[nextr][c]){//如果相等
                    game.data[r][c] *= 2;
                    game.score += game.data[r][c];
                    game.data[nextr][c] = 0;
                }
            }else{
                break;
            }
        }
    },
    moveDownNextRows:function(r,c){
        for(var i = r - 1;i >= 0;i--){

            if (game.data[i][c] != 0) {
                return i;
            }
        }
        return -1
    }
}

    game.start();
    document.onkeydown = function(e){
        var e = e || event || arguments[0];
        if(e.keyCode == 37){
            game.moveLeft();
        }else if (e.keyCode == 39) {
            game.moveRight();
        }else if (e.keyCode == 38) {
            game.moveTop();
        }else if (e.keyCode == 40) {
            game.moveDown();
        }
    }
